#ifndef _FRAME_H_
#define _FRAME_H_

#include "AniNode.h"
#include "Matrix4x4.h"


#define matrix4f Matrix4x4<float>  // Made this since all we need for this class is a 4x4 matrix of floats

class CKeyFrame : public CAniNode
{
	// Object's local matrix
	matrix4f m_LMatrix;
	
	// Object's world matrix
	//
	// If the frame has a parent, set the World Matrix to the product of the frame's World Matrix and the frame's Local Matrix -> (m_WMatrix = m_LMatrix * m_pParent->m_WMatrix)
	// Else, set the frame's World Matrix equal to the frame's Local Matrix -> (m_Matrix = m_LMatrix)
	matrix4f m_WMatrix;

	unsigned int m_flags;

public:
	enum { e_DIRTY = 1, e_REVERSE};
	CKeyFrame(void):m_flags(0){}
	~CKeyFrame(void);

	// Update "Dirties a frame and its children.  The world matrix of those frames are updated
	// and "cleaned" when the World Matrix's accesor is called.
	void Update(void)
	{
		if( 0 == (m_flags & e_DIRTY) )
		{
			m_flags |= e_DIRTY;

			for(unsigned int c = 0; c < GetChildNodes().size(); ++c)
			{
				((CKeyFrame*)GetChildNodes()[c])->Update();
			}
		}
	}

	//****************************Accessors********************************

	// Returns the local space matrix of this frame.
	matrix4f& GetLocMatrix(void){return m_LMatrix;}

	// Returns the world space matrix of this frame.
	// If the frame is dirtied, we re-calculate the frame's world matrix.

	const matrix4f& GetWorldMatrix(void)
	{
		if( (m_flags & e_DIRTY) != 0 )
		{
			if(NULL != GetParentNode())
			{
				MatrixMultiply(m_WMatrix, m_LMatrix, ((CKeyFrame*)GetParentNode())->GetWorldMatrix());
			}

			else
			{
				m_WMatrix = m_LMatrix;
			}

			m_flags &= ~e_DIRTY;
		}
		
		return m_WMatrix;

	}

};
#endif
